﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using EZCRS.HCMS.Bll.AIR;
using EZCRS.HCMS.Entity;
using System.Collections.Generic;
using EZCRS.HCMS.Bll.baseinfo;

namespace EZCRS.HCMS.Web.manage.ta
{
    public partial class channel_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (Request.QueryString["action"] != null)
                {

                    if (Request.QueryString["action"].ToLower() == "get")
                    {
                        //get data

                        int iPageIndex = 1;
                        int iPageSize = 15;

                        if (!string.IsNullOrEmpty(Request.Params["page"]))
                            int.TryParse(Request.Params["page"], out iPageIndex);

                        if (!string.IsNullOrEmpty(Request.Params["rp"]))
                            int.TryParse(Request.Params["rp"], out iPageSize);

                        Response.Write(GetJSON(iPageIndex, iPageSize, Request.Params["query"]));
                        Response.End();
                    }
                    else if (Request.QueryString["action"].ToLower() == "del")
                    {
                        //删除
                        if (!string.IsNullOrEmpty(Request.Params["id"]))
                        {
                            int iRows = DeleteChannel(Request.Params["id"]);

                            Response.Write("{state:" + iRows.ToString() + "}");

                            Response.End();
                        }
                    }
                }
            }
        }


        private int DeleteChannel(string ID)
        {
            int iChannelID = 0;

            int.TryParse(ID, out iChannelID);

            HCRS_TA_BLL tabll = new HCRS_TA_BLL();


            List<HCRS_TA> lst = tabll.GetItemsByChannel(iChannelID);

            if (lst.Count > 0)
            {
                return lst.Count;
            }

            TAChannel channel = new TAChannel();

            channel.Delete(iChannelID);

            return 0;
        }

        private string GetJSON(int PageIndex, int PageSize, string Query)
        {

            TAChannel ta = new TAChannel();

            int iRowCount = 0;
            int iPageCount = 0;

            List<Channel> lstChannels = ta.GetItems(PageIndex, PageSize, out iRowCount, out iPageCount);

            string strJson = "{\"total\":" + iRowCount + ",\"page\":" + PageIndex.ToString() + ",\"rows\":[";

            foreach (Channel cl in lstChannels)
            {
                strJson += "{\"id\":\"" + cl.ID.ToString() + "\",";
                strJson += "\"cell\":[";

                strJson += "\"" + cl.ID.ToString() + "\",";

                strJson += "\"" + HttpUtility.HtmlEncode(cl.Name) + "\",";
                strJson += "\"" + HttpUtility.HtmlEncode(cl.Code) + "\",";
                strJson += "\"<a href='ta_list.aspx?channel=" + cl.ID.ToString() + "'>查看</a>\"";
                strJson += "]},";
            }

            strJson = strJson.Replace("\r\n", " ");

            strJson = strJson.TrimEnd(',');
            strJson += "]}";

            return strJson;
        }
    }
}
